﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LogCommon;

namespace DelegetAndEventTest
{
    public class DelegateSample
    {
        public delegate bool OrderHandler(int first,int second,OrderType orderType);
        public delegate bool ComparisonHandler(int first, int second,OrderType orderType);
        public enum OrderType
        {
            Asc,
            Desc,
            ZiMu
        }
        public static int[] items;
        public static void Show()
        {
            items = new int[9];
            Console.WriteLine("请输入100以内的随机数字");
            for (int i = 0; i < 9; i++)
            {
                items[i] = int.Parse(Console.ReadLine());
            }
            Console.WriteLine("开始自动排序。。。。。");
            //BubbleSort(items);

            BubbleSort(items, OrderType.Asc, ComparRule);
            foreach (var item in items)
            {
                Console.WriteLine(item.ToString());
            }
            Console.ReadKey();
        }

        /// <summary>
        /// 排序比较规则
        /// </summary>
        /// <param name="first"></param>
        /// <param name="second"></param>
        /// <param name="orderType"></param>
        /// <returns></returns>
        private static bool ComparRule(int first,int second, OrderType orderType)
        {
            bool bFlag = false;
            try
            {
                switch (orderType)
                {
                    case OrderType.Asc:
                        bFlag = first > second;
                        break;
                    case OrderType.Desc:
                        bFlag = first < second;
                        break;
                    case OrderType.ZiMu:
                        bFlag = first.ToString().CompareTo(second.ToString()) > 0;
                        break;
                    default:
                        break;
                }
            }
            catch(Exception ex)
            {
                LogHelper.Fatal("方法： ComparRule 抛出了异常："+ ex.Message);
            }
            return bFlag;
        } 

        /// <summary>
        /// 通过委托实现的冒泡排序
        /// </summary>
        /// <param name="items"></param>
        /// <param name="orderType"></param>
        /// <param name="OnOrder"></param>
        //public static void BubbleSort(int[] items,OrderType orderType,OrderHandler OnOrder)
        //{
        //    if (items is null || items.Length <= 0)
        //    {
        //        throw new MemberAccessException();
        //    }
        //    int i, j, temp;
        //    for (i = items.Length - 1; i > 0; i--)//i 可以不用等于0
        //    {
        //        for (j = 1; j <= i; j++) //按升序排列 ，内部每次循环最大值向右移动
        //        {
        //            if (OnOrder(items[j - 1], items[j],orderType))
        //            {
        //                temp = items[j];
        //                items[j] = items[j - 1];
        //                items[j - 1] = temp;
        //            }
        //        }
        //    }
        //}


        /// <summary>
        /// 通过系统委托实现的冒泡排序
        /// 外层每次循环完成，数组的最大值或最小值都会出现在数组的最右侧。
        /// </summary>
        /// <param name="items"></param>
        /// <param name="orderType"></param>
        /// <param name="OnOrder"></param>
        public static void BubbleSort(int[] items, OrderType orderType, Func<int,int,OrderType,bool> OnOrder)
        {
            if (items is null || items.Length <= 0)
            {
                throw new MemberAccessException();
            }
            int i, j, temp;
            for (i = items.Length - 1; i > 0; i--)//i 可以不用等于0
            {
                for (j = 1; j <= i; j++) //按升序排列 ，内部每次循环最大值向右移动
                {
                    if (OnOrder(items[j - 1], items[j], orderType))
                    {
                        temp = items[j];
                        items[j] = items[j - 1];
                        items[j - 1] = temp;
                    }
                }
            }
        }

        /// <summary>
        /// 标准的冒泡排序
        /// </summary>
        /// <param name="items"></param>
        public static void BubbleSort(int[] items)
        {
            if(items is null || items.Length <= 0)
            {
                throw new MemberAccessException();
            }
            int i, j, temp;
            for(i = items.Length - 1; i > 0; i--)//i 可以不用等于0
            {
                for (j = 1; j <= i; j++) //按升序排列 ，内部每次循环最大值向右移动
                {
                    if (items[j - 1] > items[j])
                    {
                        temp = items[j];
                        items[j] = items[j - 1];
                        items[j - 1] = temp;
                    }
                }
            }
        }

        
    }
}
